package com.lichunsheng.stu.controller;

import com.lichunsheng.stu.service.PeUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private PeUserService peUserService;

    @GetMapping("/test")
    public String test() {
        return "测试数据";
    }

    @GetMapping("/login")
    public String login(String username, String password) {
        password = new Md5Hash(password, username, 3).toString();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
            if (subject.isAuthenticated()) {
                return "登录成功 id:" + subject.getSession().getId();
            } else {
                return "登录失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "账号或密码错误";
        }
    }
}
